package com.project.controller;

import com.project.bean.BuyCarBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("buyCar")
@Api(tags = "采购车")
public class BuyCarController {
    @Autowired
    private IBuyCarService buyCarService;

    @ApiOperation(value = "添加商品")
    @RequestMapping("add")
    public String add(ProductBean product, Integer buyNum, String operate, MultipartFile mf, HttpServletRequest req) throws IOException {
        if (mf != null && !mf.isEmpty()) {
            String fileName = mf.getOriginalFilename();
            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
            mf.transferTo(new File("D:/web-s/7/procure-java/procure/procureImg/" + fileName));
            product.setImgPath(fileName);
        }
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        buyCarService.add(product, buyNum, user.getUserId(), operate);
        return "ok";
    }
    @ApiOperation(value = "通过采购车id删除采购车信息")
    @RequestMapping("del")
    public String del(Integer buyCatId){
        buyCarService.del(buyCatId);
        return "ok";
    }
    @ApiOperation(value = "查询采购车信息")
    @RequestMapping("findAll")
    public List<BuyCarBean> findAll(HttpServletRequest req){
        UserBean userBean = (UserBean) req.getSession().getAttribute("loginUser");
        return buyCarService.findAll(userBean.getUserId());
    }
    @RequestMapping("updatePrice")
    @ApiOperation(value = "根据采购id修改该商品的采购的价格")
    public String updatePrice(Integer buyCatId, Integer price) {
        buyCarService.updatePrice(buyCatId, price);
        return "ok";
    }

    @RequestMapping("updateNum")
    @ApiOperation(value = "根据采购id修改该商品的采购的数量")
    public String updateNum(Integer buyCatId, Integer num) {
        buyCarService.updateNum(buyCatId, num);
        return "ok";
    }
}
